<?php
/*code here*/
/* Redirect user if they already logged */
if(Auth::hasAuth('current_user'))
{
    $this->view->current_user = Auth::getAuth('current_user');
    //header('location: /home');
    
    $db = new Database();
    $auth = Auth::getAuth('current_user');
    $userLevel = $auth['userlevel_id'];
    $getUser = $db->query("SELECT * FROM tbuser WHERE userlevel_id='$userLevel'");
    foreach($getUser as $data){
        $level = $data['userlevel_id'];
    }
    $getLevel = $db->query("SELECT * FROM tbuserlevel WHERE id='$level' and is_active='1'","array");
    foreach($getLevel as $dataLevel){
        $dbLevel = $dataLevel['userlevel'];
    }
    if($dbLevel=="Admin"){
        header('Location: /home');
    }elseif($dbLevel=="Manager"){
        header('Location: /home');
    }elseif($dbLevel=="User"){
        header('Location: /balance_inquiry');
    }
}
    $db = new Database();
    $getSettings = $db->query("SELECT * FROM tbsettings");
    foreach($getSettings as $value){
        $Invoice = $value['UpdateInvoice'];
        $Product = $value['UpdateProduct'];
        $logo = $value['logo'];
        $SystemName = $value['SystemName'];
        $footer = $value['footer'];
        $timerU = $value['Time'];
    }
/* For user login if correct redirect to home */
if($_SERVER['REQUEST_METHOD'] == "POST")
{
    $username = $_POST['username'];
    $password = $_POST['password'];
    $session = new Auth();
    $login = $session->login($username,$password,'username','password','tbuser');
    $db = new Database();
    $auth = Auth::getAuth('current_user');
    $userID = $auth['id'];
    $timezone = "Asia/Manila";
    $time = $auth['userLastLogin']; // User from their record
    
    $datetime = date('Y-m-d H:i'); // Current date
    $updated = $auth['updated'];
    
    if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
    
    if($login)
    {
        $audit = array(
                "user_id"=>$userID,
                "action_id"=>9,
                "affected_table"=>"tbuser",
                "affected_record"=>$userID,
                "datetime"=>date("Y-m-d  H:i:s"),
                "is_active"=>1
            );
        $db->insert("tbaudit_trail",$audit);
        
        if($updated=="1"){
            
        }else{
            $timeUpdated_1 = 1; // Set if user has done updating 
            $timeUpdated_0 = 0; // Set if user was nt done updating
            
            // Set Scheduler of the user to update their invoices and products
            $new_datetime = date('Y-m-d H:i', strtotime("+$timerU hours", strtotime($datetime))); // Add hour based on user settings set up
            $condition = array("id"=>$userID);
            $fields = array("updated"=>$timeUpdated_1,
                            "updateTime"=>$new_datetime);
                $db->update("tbuser",$fields,$condition);
        }
        
        
        
        
        $login = $db->query("SELECT *
                                    FROM tbuser
                                    WHERE id={$db->escape($userID)} ","row");
        Auth::setAuth('current_user',$login);
        //header('Location: /home');
        
        $userLevel = $auth['userlevel_id'];
        $getUser = $db->query("SELECT * FROM tbuser WHERE userlevel_id='$userLevel'");
        foreach($getUser as $data){
            $level = $data['userlevel_id'];
        }
        $getLevel = $db->query("SELECT * FROM tbuserlevel WHERE id='$level' and is_active='1'","array");
        foreach($getLevel as $dataLevel){
            $dbLevel = $dataLevel['userlevel'];
        }
        if($dbLevel=="Admin"){
            header('Location: /home');
        }elseif($dbLevel=="Manager"){
            header('Location: /home');
        }elseif($dbLevel=="User"){
            header('Location: /balance_inquiry');
        }
    }
}   
/*code here(end)*/
$this->setLayout();